library(readxl)
library(readr)
library(dplyr)
library(ggplot2)
library(stringr)
library(coefplot)
library(tidyr)
library(xtable)
library(stargazer)
library(ggpubr)
library(estimatr)

#------------------------------------------------------------------------------------------
## Method 4. ANTUSD Sentiment Scores Using CMCOR Data
#------------------------------------------------------------------------------------------

# Load files

# Mobilization Campaigns
India_1962 <- read_excel("India_1962.xlsx", range = cell_cols("B:V"))
Soviet_1969 <- read_excel("Soviet_1969.xlsx", range = cell_cols("B:V"))
Vietnam_1974 <- read_excel("Vietnam_1974.xlsx", range = cell_cols("B:V"))
Vietnam_1979 <- read_excel("Vietnam_1979.xlsx", range = cell_cols("B:V"))

# Pacification Campaigns
Japan_1990 <- read_excel("Japan_1990.xlsx", range = cell_cols("B:V"))
Japan_1996 <- read_excel("Japan_1996.xlsx", range = cell_cols("B:V"))
Japan_2005 <- read_excel("Japan_2005.xlsx", range = cell_cols("B:V"))
Japan_2010 <- read_excel("Japan_2010.xlsx", range = cell_cols("B:V"))
Japan_2012 <- read_excel("Japan_2012.xlsx", range = cell_cols("B:V"))
Philippines_2016 <- read_excel("Philippines_2016.xlsx", range = cell_cols("B:V"))
India_2017 <- read_excel("India_2017.xlsx", range = cell_cols("B:V"))

# Combined by type of campaigns
Mob <- rbind(India_1962,Soviet_1969,Vietnam_1974,Vietnam_1979)
Pac <- rbind(Japan_1990,Japan_1996,Japan_2005,Japan_2010,Japan_2012,Philippines_2016,India_2017)

# ANTUSD Sentiment Scores
# Jump to line 115 if want to use outputs directly.
ANTUSD <- read.csv("ANTUSD.csv",header = FALSE)# select the sentiment dic in csv file

Row_Mob <- as.data.frame(1:dim(Mob)[1]) # Almost 4 hours
for (i in 1:dim(Mob)[1]){
  x = Mob[i,]
  for (n in 1:length(x)){
    Row_Mob[i,n+1] <- match(x[n],ANTUSD$V1)
  }
}

Row_Pac <- as.data.frame(1:dim(Pac)[1]) # Less than 30 mins
for (i in 1:dim(Pac)[1]){
  x = Pac[i,]
  for (n in 1:length(x)){
    Row_Pac[i,n+1] <- match(x[n],ANTUSD$V1)
  }
}

Row_Mob <- Row_Mob[,-1]
write.csv(Row_Mob,file = "ANTUSD_Row_Mob_SW.csv")
Row_Pac <- Row_Pac[,-1]
write.csv(Row_Pac,file = "ANTUSD_Row_Pac_SW.csv")

Score_Mob <- as.data.frame(1:dim(Mob)[1])
for (i in 1:dim(Row_Mob)[1]){
  x = Row_Mob[i,]
  for (n in 1:length(x)){
    Score_Mob[i,n+1] <- ANTUSD_Score[Row_Mob[i,n]]
  }
}

Score_Pac <- as.data.frame(1:dim(Pac)[1])
for (i in 1:dim(Row_Pac)[1]){
  x = Row_Pac[i,]
  for (n in 1:length(x)){
    Score_Pac[i,n+1] <- ANTUSD_Score[Row_Pac[i,n]]
  }
}

Score_Mob <- Score_Mob[,-1]
write.csv(Score_Mob,file = "ANTUSD_Score_Mob_SW.csv")
Score_Mob[is.na(Score_Mob)] <- 0
Score_Mob <- mutate_all(Score_Mob, function(x) as.numeric(as.character(x)))
ANTUSD_Score_Mob <- rowSums(Score_Mob)
ANTUSD_Score_Mob <- as.data.frame(ANTUSD_Score_Mob)
colnames(ANTUSD_Score_Mob) <- c("Score")
India1962_Vec <- rep("India_1962",dim(India_1962)[1])
Soviet1969_Vec <- rep("Soviet_1969",dim(Soviet_1969)[1])
Vietnam1974_Vec <- rep("Vietnam_1974",dim(Vietnam_1974)[1])
Vietnam1979_Vec <- rep("Vietnam_1979",dim(Vietnam_1979)[1])
Mob_Campaign <- c(India1962_Vec,Soviet1969_Vec,Vietnam1974_Vec,Vietnam1979_Vec)
ANTUSD_Score_Mob$Campaigns <- Mob_Campaign
ANTUSD_Score_Mob$Type <- "Mobilization Campaign"
write.csv(ANTUSD_Score_Mob,file = "ANTUSD_Score_Mob.csv")

Score_Pac <- Score_Pac[,-1]
write.csv(Score_Pac,file = "ANTUSD_Score_Pac_SW.csv")
Score_Pac[is.na(Score_Pac)] <- 0
Score_Pac <- mutate_all(Score_Pac, function(x) as.numeric(as.character(x)))
ANTUSD_Score_Pac <- rowSums(Score_Pac)
ANTUSD_Score_Pac <- as.data.frame(ANTUSD_Score_Pac)
colnames(ANTUSD_Score_Pac) <- c("Score")
Japan1990_Vec <- rep("Japan_1990",dim(Japan_1990)[1])
Japan1996_Vec <- rep("Japan_1996",dim(Japan_1996)[1])
Japan2005_Vec <- rep("Japan_2005",dim(Japan_2005)[1])
Japan2010_Vec <- rep("Japan_2010",dim(Japan_2010)[1])
Japan2012_Vec <- rep("Japan_2012",dim(Japan_2012)[1])
Philippines2016_Vec <- rep("Philippines_2016",dim(Philippines_2016)[1])
India2017_Vec <- rep("India_2017",dim(India_2017)[1])
Pac_Campaign <- c(Japan1990_Vec,Japan1996_Vec,Japan2005_Vec,Japan2010_Vec,Japan2012_Vec,
                  Philippines2016_Vec,India2017_Vec)
ANTUSD_Score_Pac$Campaigns <- Pac_Campaign
ANTUSD_Score_Pac$Type <- "Pacification Campaign"
write.csv(ANTUSD_Score_Pac,file = "ANTUSD_Score_Pac.csv")

# ==================== Table 7.8 =====================================================
ANTUSD_Score_Mob <- read_csv("ANTUSD_Score_Mob.csv")
ANTUSD_Score_Pac <- read_csv("ANTUSD_Score_Pac.csv")

ANTUSD_Mob_mean <- ANTUSD_Score_Mob %>%
  summarise(mob_ANTUSD_average=mean(ANTUSD_Score))

ANTUSD_Pac_mean <- ANTUSD_Score_Pac %>%
  summarise(pac_ANTUSD_average=mean(ANTUSD_Score))

mean_data <- cbind(ANTUSD_Mob_mean$mob_ANTUSD_average,ANTUSD_Pac_mean$pac_ANTUSD_average)
colnames(mean_data) <- c("Mobilization Campaign","Pacification Campaign")
rownames(mean_data) <- c("Sentiment Score")
# xtable(mean_data) # Same with stargazer 
stargazer(mean_data, type = 'latex', title = 'Average of sentiment scores') #Put Latex codes into Latex to generate table


